package org.xserver.component.core.dispatch.http;

import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HttpRequestDispatchFactory {

	private static final Logger log = LoggerFactory
			.getLogger(HttpRequestDispatchFactory.class);

	public static Map<String, HttpRequestDispatch> httpRequestDispatchInfos = new HashMap<String, HttpRequestDispatch>();

	static {
		for (HttpRequestType httpRequestType : HttpRequestType.values()) {
			try {
				httpRequestDispatchInfos.put(httpRequestType.getType(),
						httpRequestType.getClazz().newInstance());
			} catch (Exception e) {
				log.error("init writeHandlerInfos error", e);
				System.exit(-1);
			}
		}
	}

	/**
	 * Prototype between client and server
	 */
	public enum HttpRequestType {
		FILE("FILE", FileRequestDispatch.class), 
		INTERFACE("INTERFACE",InterfaceRequestDispatch.class),
		WEBSOCKET("WEBSOCKET",WebSocketRequestDispatch.class);

		private final String type;

		private final Class<? extends HttpRequestDispatch> clazz;

		public String getType() {
			return type;
		}

		public Class<? extends HttpRequestDispatch> getClazz() {
			return clazz;
		}

		HttpRequestType(String type, Class<? extends HttpRequestDispatch> clazz) {
			this.type = type;
			this.clazz = clazz;
		}
	}

	public static HttpRequestDispatch getInstance(String key) {
		return httpRequestDispatchInfos.get(key);
	}

}
